package com.common.handler;

import cn.hutool.json.JSONObject;
import com.common.groovy.adapter.GroovyModelAdapter;
import com.common.model.SystemApplicationServiceModel;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;

/**
 * @Description:
 * @Date: 2025/7/3 22:06
 */
public abstract class HttpMethodHandler<P> {

    @Autowired
    protected GroovyModelAdapter modelAdapter;


    public abstract boolean validateMethod(String method);

    public abstract P parseParams(ServletRequest servletRequest, ServletResponse servletResponse);

    public Object handler(ServletRequest servletRequest, ServletResponse servletResponse, SystemApplicationServiceModel applicationServiceModel) throws Exception {
        P params = parseParams(servletRequest, servletResponse);
        JSONObject data = applicationServiceModel.getData();
        String modelName = data.getStr("modelName");
        String action = data.getStr("action");
        return modelAdapter.call(modelName, action, params);
    }
}
